package ar.utn.jess.servlet;

import java.io.IOException;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import ar.utn.jess.common.AbstractEntity;
import ar.utn.jess.common.EditorData;
import ar.utn.jess.common.EntitySelector;
import ar.utn.jess.common.SelectorData;
import ar.utn.jess.common.Util;
import ar.utn.jess.domain.Viaje;
import ar.utn.jess.exception.JSSException;

public class ReasignarServlet extends BaseServlet {

	private static final long serialVersionUID = 1L;

	@Override
	protected void serve(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {

		String action = req.getParameter("action");
		
		if(action==null) {
			
//			req.setAttribute("entity", Util.createAbstractEntity("chofer"));
			
			RequestDispatcher rd = getServletContext().getRequestDispatcher("/WEB-INF/pages/reasignar.jsp");		
			rd.forward(req, resp);
		}
		else if(action.equals("ver")) {
			
			String id = req.getParameter("id");
			
			Viaje viaje = Viaje.findById(Viaje.class, new Integer(id));
			viaje.loadChofer();
			
			req.setAttribute("entity", viaje);
			
			RequestDispatcher rd = getServletContext().getRequestDispatcher("/WEB-INF/pages/reasignar.jsp?ii=1");		
			rd.forward(req, resp);
		}
		else if(action.equals("getForGrid")) {
			getForGrid(req, resp);
		}
	}

	
	@SuppressWarnings("unchecked")
	protected EditorData buildEditorData(HttpServletRequest req) {
		EditorData editorData = new EditorData();
		
		Enumeration<String> en = req.getParameterNames();
		while(en.hasMoreElements()) {
			String name = en.nextElement();
			editorData.addAttribute(name, req.getParameter(name));
		}
		
		return editorData;
	}
	
	protected void getForGrid(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String entityName = req.getParameter("entity");		
		
		try {
			final AbstractEntity entity = Util.createAbstractEntity(entityName);
			
			String json = ServletUtils.getJsonForJQGrid(req, entity, new EntitySelector() {
				@SuppressWarnings({ "unchecked", "static-access" })
				public List<HashMap<String, Object>> getRows(Class clazz, SelectorData selectorData) {
					
					System.out.println(entity.findForSelector(clazz, selectorData));
					return entity.findForSelector(clazz, selectorData);
				}
				
				@SuppressWarnings({ "unchecked", "static-access" })
				@Override
				public Integer getTotalRows(Class clazz, SelectorData selectorData) {
					return entity.getCountForSelector(clazz, selectorData);
				}
			});
											
			resp.setContentType("application/json;charset=utf-8");
			resp.getWriter().write(json);
		}
		catch(Exception e) {
			throw new JSSException(e);
		}						
	}
	
	private void autosActivosGrid(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		
		try {
			final AbstractEntity entity = Util.createAbstractEntity("autoActivo");
			
			String json = ServletUtils.getJsonForJQGrid(req, entity, new EntitySelector() {
				@SuppressWarnings({ "unchecked", "static-access" })
				public List<HashMap<String, Object>> getRows(Class clazz, SelectorData selectorData) {
					return entity.findForSelector(clazz, selectorData);
				}
				
				@SuppressWarnings({ "unchecked", "static-access" })
				@Override
				public Integer getTotalRows(Class clazz, SelectorData selectorData) {
					return entity.getCountForSelector(clazz, selectorData);
				}
			});
											
			resp.setContentType("application/json;charset=utf-8");
			resp.getWriter().write(json);
		}
		catch(Exception e) {
			throw new JSSException(e);
		}						
	}
}
